/* your js go here */
$(function () {
    var basePath = $("#contextPath").val();
    var userId = $("#myUserId").val();
    $("#avatar").click(function () {
        console.log("你点击了头像");
        window.location.href = basePath + '/view/userLook?uId=' + userId;
    });

    //file表单选中文件时,让file表单的val展示到showname这个展示框
    $('#myFile').change(function () {
        // $('#showname').val($(this).val());
        var file = $("#myFile")[0].files[0];
        var formData = new FormData();
        formData.append("file", file);
        $.ajax({
            url: basePath + "/user/uploadFile",
            type: "post",
            data: formData,
            processData: false, // 使数据不做处理
            contentType: false, // 不要设置Content-Type请求头
            success: function (data) {
                if ("fail" !== data) {
                    $("#headPic").attr("src", data);
                    $("#myPic").attr("src", data);
                }
            },
            error: function (response) {
                console.log(response);
            }
        });

    });

    // 保存用户信息
    $("#userInfo").click(function () {
        var list = $("#userInfoList");
        console.log(list);
        $.ajax({
            type: "post",
            url: basePath + "/user/updateUserInfo",
            data: list.serialize(),
            processData: false, // 使数据不做处理
            success: function (data) {
                if ("success" === data) {
                    // layer.msg("保存成功");
                    alert("保存成功");
                    window.location.reload();
                } else {
                    layer.msg("保存失败");
                }
            }
        });
    });

    //用户列表，查看详情，通过class名字userDetail来找
    $(".userDetail").click(function () {
        console.log("你点击了用户详情");
        // window.location.href = '../../html/user_detail.jsp'
        var uId = $(this).attr("data-id");
        var path = basePath + "/view/userLook";
        window.location.href = basePath + "/view/userDetail?uId=" + uId;
    });

    //发布会议跳转链接
    $("#addMeet").click(function () {
        window.location.href = basePath + "/view/writeMeeting";
    });

    //添加文章跳转链接
    $("#addArt").click(function () {
        window.location.href = basePath+"/view/writeArticle";
    });


    /**
     * 隐藏部门列表的逻辑
     */
    $(".myDepList>ul>li").on("click", function () {
        if ($(this).find("div").attr('class').indexOf("isHidden") !== -1) {
            $(this).find("div").removeClass("isHidden");
        } else {
            $(this).find("div").addClass("isHidden");
        }


    });


    /**
     * 隐藏用户列表的逻辑
     */
    $(".myList>ul>li>div").on("click", function () {
        var ufId = $(this).children("input").attr("data-id");
        if ($(this).attr('class').indexOf("isHidden") != -1) {
            $(this).parent().removeClass("isHidden");
        } else {
            $(this).parent().addClass("isHidden");
            window.location.href = basePath + "/view/myFocusList?ufId=" + ufId;
        }
    });

    $(".table").find("input[type='checkbox']").on("click", function () {
        var ufId = $(this).attr("data-uid");
        if ($(this).prop("checked")) {
            var path = basePath + "/user/follow";
            if (ufId === userId) {
                $(this).click();
                layer("不能关注自己");
            } else {
                $.post(path, {ufId: ufId}, function (data) {
                    if (data === "success") {
                        layer.msg("关注成功");
                    } else {
                        layer.msg(data);
                    }
                });
            }
        } else {
            var unpath = basePath + "/user/unfollow";
            if (ufId === userId) {
                layer.msg("不能关注自己");
            } else {
                $.post(unpath, {ufId: ufId}, function (data) {
                    if (data === "success") {
                        layer.msg("取关成功");
                    } else {
                        layer.msg(data);
                    }
                });
            }
        }
    });

    // 添加收藏或者取消收藏
    $(".myCollect").click(function () {
        var value = $(this).val();
        var aId = $(this).attr("data-aid");
        var userId = $("#myUserId").val();
        if (value === "取消收藏") {
            var rePath = basePath + "/article/removeCollect";
            $(this).val("添加收藏");
            $.post(rePath, {aId: aId}, function (data) {
                if (data === "success") {
                    console.log(111);
                    layer.msg("成功取消收藏");
                } else {
                    layer.msg(data);
                }
            });
        } else if (value === "添加收藏") {
            var addPath = basePath + "/article/addCollect";
            $(this).val("取消收藏");
            $.post(addPath, {aId: aId}, function (data) {
                if (data === "success") {
                    layer.msg("成功添加收藏");
                } else {
                    layer.msg(data);
                }
            });
        }
    });


    // 点击取消会议
    $(".myTitle .cancel").click(function () {
        var mId = $(this).attr("data-id");
        var canPath = basePath + "/meet/cancelMeeting";
        $.post(canPath,{mId:mId},function (data) {
            if (data === "success") {
                var b= $(".myTitle .cancel");
                b.attr("disabled","disables");
                b.val("已取消会议");
            }else {
                layer.msg(data);
            }
        })
    });


    // 点击参加会议
    $(".myTitle .join").click(function () {
        var mId = $(this).attr("data-id");
        var joinPath = basePath + "/meet/joinMeeting";
        $.post(joinPath,{mId:mId},function (data) {
            if (data === "success") {
                var b= $(".myTitle .join");
                b.attr("disabled","disables");
                b.val("已确认参加会议");
            }else {
                layer.msg(data);
            }
        })
    });

    //页码最前最后一页提示
    $("#firstPage").click(function () {
        alert("已经到头了");
    });
    $("#lastPage").click(function () {
        alert("后面没有了");
    });

});

//触发隐藏的file表单
function makeThisfile() {
    $('#myFile').click();
}


